/*!
 * Page: pages/activity/location/Location
 * License: MIT
 * Created: 2019-11-29 16:00
 */

/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */

Page({
  data: {
    region: [],
    customItem: '全部',
    detail: '',
  },
  bindRegionChange(e: WechatMiniprogram.CustomEvent) {
    const {
      detail: { value },
    } = e
    console.log('-----------', value)

    this.setData({
      region: value,
    })
  },
  /** 详细地址 */
  inputDetail(e: WechatMiniprogram.CustomEvent) {
    const {
      detail: { value },
    } = e
    this.setData({
      detail: value,
    })
  },
  beSure() {
    if (this.data.region.length < 1) {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 1000,
      })
      return
    }
    const pages = getCurrentPages()
    const lastPage = pages[pages.length - 2]
    let resultLocation = ''

    if (this.data.region[0] != '全部') {
      resultLocation += this.data.region[0]
    }
    if (this.data.region[1] != '全部') {
      resultLocation += this.data.region[1]
    }
    if (this.data.region[2] != '全部') {
      resultLocation += this.data.region[2]
    }

    lastPage.setData({
      location: resultLocation,
      address: this.data.detail,
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  /** 打开地图 */
  openLocation() {
    wx.chooseLocation({
      success: function (res) {
        const address = res.address as any
        const proExp = '.+[省]',
          cityExp = '.+[市]'
        const province: any = address.match(new RegExp(proExp)) // 省
        const city = address.match(new RegExp(cityExp))[0].replace(province, '') // 市
        const pages = getCurrentPages()
        const lastPage = pages[pages.length - 2]
        lastPage.setData({
          location: `${province}${city}`,
          address: res.name,
        })
        wx.navigateBack({
          delta: 1,
        })
      },
    })
  },
})
